<?php

// +----------------------------------------------------------------------
// | Easyadmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\apply;

use app\admin\model\website\UserSign as UserSignModel;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\facade\Db;
use think\App;

/**
 * Class Admin
 * @package app\admin\controller\user
 * @ControllerAnnotation(title="站点管理")
 */
class UserSign extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new UserSignModel();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            
            foreach ($where as &$item) {
                if ($item[0] == 'username') {
                    $item[0] = 'u.username';
                }else {
                    $item[0] = 's.'.$item[0];
                }
            }

            $countModel = $this->model;

            if (session("admin.group") != 1 && session("admin.group") != '5' && session("admin.group") != '6') {
                $countModel->where('u.extension', session("admin.username"));
            }else if (session("admin.group") != 1 && session("admin.group") != '5') {
                $countModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $countModel->where("u.group", "not in", "1,4,5,6");
            } else if (session('admin.group') == 5) {
                $countModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $countModel->where("u.group", "not in", "1,4,5");
            }

            $listModel = $this->model;
            if (session("admin.group") != 1 && session("admin.group") != '5' && session("admin.group") != '6'){
                $listModel->where('u.extension', session("admin.username"));
            }else if (session("admin.group") != 1 && session("admin.group") != '5') {
                $listModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $listModel->where("u.group", "not in", "1,4,5,6");
            } else if (session('admin.group') == 5) {
                $listModel->where('u.rooms', 'in', explode(",", session("admin.rooms")));
                $listModel->where("u.group", "not in", "1,4,5");
            }

            $count = $countModel
            ->alias("s")
            ->join('users u','u.id = s.uid')
            ->where("s.uid",'<>',2)
            ->where($where)
            ->count();


            $list = $listModel
                ->alias("s")
                ->join('users u','u.id = s.uid')
                ->where($where)
                ->where("s.uid",'<>',2)
                ->page($page, $limit)
                ->field("u.username,s.*")
                ->order($this->sort)
                ->select()->each(function ($item) {
                    $item['add_time'] = date("Y-m-d H:i",$item['add_time']);
                });
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}
